<!-- 发起退换票 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="800px"
    @close="handleClose"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <!-- 退换票信息展示 -->
      <div class="apply-info-container">
        <h3>退换票申请信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <span class="label">发票种类：</span>
            <span class="value">
              <span>{{
                dict.invoice_category[Number(formData.invoiceCategory)]
              }}</span>
            </span>
          </div>
          <div class="info-item">
            <span class="label">发票类型：</span>
            <span class="value">
              <span>{{ dict.invoice_type[Number(formData.invoiceType)] }}</span>
            </span>
          </div>
          <div class="info-item">
            <span class="label">姓名：</span>
            <span class="value">{{ formData.studentName }}</span>
          </div>
          <div class="info-item">
            <span class="label">身份证号：</span>
            <span class="value">{{ formData.idNumber }}</span>
          </div>
          <div class="info-item">
            <span class="label">单位：</span>
            <span class="value">{{ formData.unitName }}</span>
          </div>
          <div class="info-item">
            <span class="label">单位统一信用代码：</span>
            <span class="value">{{ formData.socialCreditCode }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系方式：</span>
            <span class="value">{{ formData.phonenumber }}</span>
          </div>
          <div class="info-item">
            <span class="label">发票金额：</span>
            <span class="value">{{ formData.invoiceAmount }}</span>
          </div>
          <div class="info-item">
            <span class="label">发票编号：</span>
            <span class="value">{{ formData.invoiceNumber }}</span>
          </div>
        </div>
      </div>

      <!-- 申请原因 -->
      <el-form-item label="申请原因" prop="exchangeReason">
        <el-input
          v-model="formData.exchangeReason"
          type="textarea"
          :rows="4"
          placeholder="请输入申请原因"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "StartExchangeDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    exchangeInfo: {
      type: Object,
      default: () => ({}),
    },
    dict: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      formData: {
        invoiceCategory: "",
        invoiceType: "",
        studentName: "",
        idNumber: "",
        unitName: "",
        socialCreditCode: "",
        phonenumber: "",
        invoiceAmount: "",
        invoiceNumber: "",
        exchangeReason: "",
      },
      rules: {
        exchangeReason: [
          { required: true, message: "请输入申请原因", trigger: "blur" },
        ],
      },
      title: "发起退换票",
    };
  },
  watch: {
    // 监听 exchangeInfo 变化，更新表单数据
    exchangeInfo: {
      handler(newVal) {
        if (newVal) {
          this.formData = { ...newVal };
        }
      },
      deep: true,
    },
  },
  methods: {
    // 关闭对话框
    handleClose() {
      this.$emit("close");
      this.resetForm();
    },

    // 提交处理
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$emit("submit", this.formData);
          this.handleClose();
        }
      });
    },

    // 重置表单
    resetForm() {
      this.formData = {
        invoiceCategory: "",
        invoiceType: "",
        studentName: "",
        idNumber: "",
        unitName: "",
        socialCreditCode: "",
        phonenumber: "",
        invoiceAmount: "",
        invoiceNumber: "",
        exchangeReason: "",
      };
    },
  },
};
</script>

<style scoped>
.apply-info-container {
  border: 1px solid #dcdfe6;
  padding: 20px;
  margin-bottom: 20px;
}

.apply-info-container h3 {
  margin-top: 0;
  color: #303133;
  font-size: 16px;
  margin-bottom: 15px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.label {
  font-weight: bold;
  min-width: 120px;
  color: #909399;
}

.value {
  flex: 1;
  color: #303133;
}

.dialog-footer {
  text-align: right;
}
</style>